package com.xunbao.controller;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;

/**
 * Created by YiJiaWei on 2016/5/11.
 */

@Controller
public class UpLoadController {


    @RequestMapping(value = "/batchAgentPay")
    @ResponseBody
    public String batchAgentPay(@RequestParam("upLoadFile") MultipartFile upLoadFile) {

        if (upLoadFile != null) {
            String fileName = upLoadFile.getName();
            System.out.println("****fileName:" + fileName);
            System.out.println("****originalFilename:" + upLoadFile.getOriginalFilename());


            try {


//                System.out.println("isExcel2003 = " + isExcel2003(upLoadFile.getInputStream()));
//                System.out.println("isExcel2007 = " + isExcel2007(upLoadFile.getInputStream()));

                InputStream inputStream = upLoadFile.getInputStream();

                Workbook workbook = create(inputStream);

                for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
                    Sheet hssfSheet = workbook.getSheetAt(numSheet);
                    if (hssfSheet == null) {
                        continue;
                    }
                    // 循环行Row
                    for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                        System.out.println("第" + rowNum + "行---");
                        Row hssfRow = hssfSheet.getRow(rowNum);
                        if (hssfRow == null) {
                            continue;
                        }
                        for (int columnNum = 0; columnNum < hssfRow.getLastCellNum(); columnNum++) {
                            Cell cell = hssfRow.getCell(columnNum);
                            if (cell == null) {
                                continue;
                            } else {
                                System.out.println("sName:" + cell);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }


        }
        return "batchAgentPay - OK";
    }

    public Workbook create(InputStream inp) throws IOException, InvalidFormatException {
        if (!inp.markSupported()) {
            inp = new PushbackInputStream(inp, 8);
        }
        if (POIFSFileSystem.hasPOIFSHeader(inp)) {
            return new HSSFWorkbook(inp);
        }
        if (POIXMLDocument.hasOOXMLHeader(inp)) {
            return new XSSFWorkbook(inp);
        }
        throw new IllegalArgumentException("你的excel版本目前poi解析不了");
    }

}
